diff options
Diffstat (limited to '')
| -rw-r--r-- | docs/using_luarocks.md | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/docs/using_luarocks.md b/docs/using_luarocks.md new file mode 100644 index 00000000..c65ef432 --- /dev/null +++ b/docs/using_luarocks.md | |||
| @@ -0,0 +1,198 @@ | |||
| 1 | # Using LuaRocks | ||
| 2 | |||
| 3 | So, you have followed the installation instructions (either on | ||
| 4 | [Unix](installation_instructions_for_unix.md) or | ||
| 5 | [Windows](installation_instructions_for_windows.md)) and now you have LuaRocks | ||
| 6 | installed on your machine. Now you probably want to install some rocks | ||
| 7 | (packages containing Lua modules) and use them in your Lua code. | ||
| 8 | |||
| 9 | For LuaRocks to function properly, we have a quick checklist to go through | ||
| 10 | first: | ||
| 11 | |||
| 12 | # Command-line tools (and the system path) | ||
| 13 | |||
| 14 | LuaRocks installs some command-line tools which are your interface for | ||
| 15 | managing your rocks: [luarocks](luarocks.md) and | ||
| 16 | [luarocks-admin](luarocks_admin.md). Make sure the directory where they are | ||
| 17 | located is in your PATH -- the exact location depends on the flags you gave | ||
| 18 | when installing LuaRocks. | ||
| 19 | |||
| 20 | Run [luarocks](luarocks.md) to see the available commands: | ||
| 21 | |||
| 22 | ``` | ||
| 23 | luarocks | ||
| 24 | ``` | ||
| 25 | |||
| 26 | You can get help on any command by using the [luarocks help](luarocks_help.md) command: | ||
| 27 | |||
| 28 | ``` | ||
| 29 | luarocks help install | ||
| 30 | ``` | ||
| 31 | |||
| 32 | Installing packages is done by typing commands such as: | ||
| 33 | |||
| 34 | ``` | ||
| 35 | luarocks install dkjson | ||
| 36 | ``` | ||
| 37 | |||
| 38 | # Rocks trees and the Lua libraries path | ||
| 39 | |||
| 40 | When you install rocks using the `luarocks install`, you get new modules | ||
| 41 | available for loading via `require()` from Lua. For example, after we install | ||
| 42 | the dkjson rock, type `luarocks show dkjson` to show the module installed by | ||
| 43 | the rock: | ||
| 44 | |||
| 45 | ``` | ||
| 46 | luarocks show dkjson | ||
| 47 | ``` | ||
| 48 | |||
| 49 | This should output something like this: | ||
| 50 | |||
| 51 | ``` | ||
| 52 | dkjson 2.5-2 - David Kolf's JSON module for Lua | ||
| 53 | |||
| 54 | dkjson is a module for encoding and decoding JSON data. It supports UTF-8. | ||
| 55 | |||
| 56 | JSON (JavaScript Object Notation) is a format for serializing data based on the | ||
| 57 | syntax for JavaScript data structures. | ||
| 58 | |||
| 59 | dkjson is written in Lua without any dependencies, but when LPeg is available | ||
| 60 | dkjson uses it to speed up decoding. | ||
| 61 | |||
| 62 | License: MIT/X11 | ||
| 63 | Homepage: http://dkolf.de/src/dkjson-lua.fsl/ | ||
| 64 | Installed in: /usr/local | ||
| 65 | |||
| 66 | Modules: | ||
| 67 | dkjson (/usr/local/share/lua/5.3/dkjson.lua) | ||
| 68 | ``` | ||
| 69 | |||
| 70 | It presents a short description of the rock, its license, and the list of | ||
| 71 | modules it provides (in this case, only one, `dkjson`). Note that "Installed | ||
| 72 | in:" shows the directory tree where the rock was installed. This is the "rocks | ||
| 73 | tree" in use. | ||
| 74 | |||
| 75 | Most LuaRocks installations will feature two rocks trees: | ||
| 76 | |||
| 77 | * "system" [rock tree](rocks_repositories.md) (used by default) | ||
| 78 | * "user" [rock tree](rocks_repositories.md) | ||
| 79 | |||
| 80 | To be able to use the module, we need to make sure that Lua can find that | ||
| 81 | dkjson.lua file when we run `require("dkjson")`. You can check your Lua paths | ||
| 82 | from the Lua environment, using | ||
| 83 | |||
| 84 | ``` | ||
| 85 | print(package.path) | ||
| 86 | print(package.cpath) | ||
| 87 | ``` | ||
| 88 | |||
| 89 | These variables can be pre-configured from outside Lua, using the LUA_PATH and | ||
| 90 | LUA_CPATH environment variables. | ||
| 91 | |||
| 92 | If you installed both Lua and LuaRocks in their default directories | ||
| 93 | (/usr/local on Linux and Mac OSX), then the "system" tree is /usr/local and it | ||
| 94 | will work by default. However, the "user" tree (for installing rocks without | ||
| 95 | admin privileges) is not detected by Lua by default. For that we'll need to | ||
| 96 | configure these environment variables. | ||
| 97 | |||
| 98 | LuaRocks offers a semi-automated way to do this. If you type the following | ||
| 99 | command: | ||
| 100 | |||
| 101 | ``` | ||
| 102 | luarocks path --bin | ||
| 103 | ``` | ||
| 104 | |||
| 105 | ...it will print commands suitable for your platform for setting up your | ||
| 106 | environment. On typical Unix terminal environments, you can type this: | ||
| 107 | |||
| 108 | ``` | ||
| 109 | eval "$(luarocks path --bin)" | ||
| 110 | ``` | ||
| 111 | |||
| 112 | and it apply the changes, temporarily, to your shell. To have these variables | ||
| 113 | set permanently, you have to configure the environment variables to your shell | ||
| 114 | configuration (for example, by adding the above line to your `.bashrc` file if | ||
| 115 | your shell is Bash). | ||
| 116 | |||
| 117 | # Multiple versions using the LuaRocks package loader | ||
| 118 | |||
| 119 | If you want to make use of LuaRocks' support for multiple installed versions | ||
| 120 | of modules, you need to load a custom package loader: luarocks.loader. | ||
| 121 | |||
| 122 | You should be able to launch the Lua interpreter with the LuaRocks-enabled | ||
| 123 | loader by typing: | ||
| 124 | |||
| 125 | ``` | ||
| 126 | lua -lluarocks.loader | ||
| 127 | ``` | ||
| 128 | |||
| 129 | Alternatively, you can load the LuaRocks module loader from Lua by issuing | ||
| 130 | this command: | ||
| 131 | |||
| 132 | ``` | ||
| 133 | require "luarocks.loader" | ||
| 134 | ``` | ||
| 135 | |||
| 136 | If your system is correctly set up so that this command runs with no errors, | ||
| 137 | subsequent calls to `require()` are LuaRocks-aware and the exact version of | ||
| 138 | each module will be determined based on the dependency tree of previously | ||
| 139 | loaded modules. | ||
| 140 | |||
| 141 | # Scripts installed by rocks (and the scripts path) | ||
| 142 | |||
| 143 | Besides modules, rocks can also install command-line scripts. The default | ||
| 144 | location of this directory (unless you configured your local repository | ||
| 145 | differently) is /usr/local/bin for system-wide installs and ~/.luarocks/bin | ||
| 146 | for per-user installs on Unix and %APPDATA%/luarocks/bin on Windows -- make | ||
| 147 | sure it is in your PATH as well. | ||
| 148 | |||
| 149 | If you use the `--bin` argument in `luarocks path`, it will also print the | ||
| 150 | appropriate PATH configuration: | ||
| 151 | |||
| 152 | ``` | ||
| 153 | luarocks path --bin | ||
| 154 | ``` | ||
| 155 | |||
| 156 | # Using in Unix systems with sudo | ||
| 157 | |||
| 158 | When you use LuaRocks to install a package while you aren't root, the package | ||
| 159 | will get installed in $HOME/.luarocks/ instead of the system-wide (by default, | ||
| 160 | /usr/local/) and become only available for you. Moreover Lua doesn't know with | ||
| 161 | its default setup that packages can be available in the current user's home. | ||
| 162 | If you want to install a package available for all users, you should run it as | ||
| 163 | superuser, typically using sudo. | ||
| 164 | |||
| 165 | For example: | ||
| 166 | |||
| 167 | ``` | ||
| 168 | sudo luarocks install stdlib | ||
| 169 | ``` | ||
| 170 | |||
| 171 | After that, some files may not have correct permissions. For example, if | ||
| 172 | /usr/local/share/lua/5.1/base.lua is only readable by root user, you should at | ||
| 173 | least set them to readable for all users (chmod a+r or chmod 644). | ||
| 174 | |||
| 175 | For example: | ||
| 176 | |||
| 177 | ``` | ||
| 178 | cd /usr/local/share/lua/5.1 | ||
| 179 | sudo chmod a+r * | ||
| 180 | ``` | ||
| 181 | |||
| 182 | # Using a C compiler | ||
| 183 | |||
| 184 | Because rocks are generally available in the repository as [source | ||
| 185 | rocks](types_of_rocks.md) rather than binary rocks, it is best to have a C | ||
| 186 | compiler available. | ||
| 187 | |||
| 188 | On Windows, MinGW and Microsoft compilers are supported. The compiler should | ||
| 189 | be in the system path, or explicitly configured in the LuaRocks config files. | ||
| 190 | On Windows systems, one way of getting the compiler in the system path is to | ||
| 191 | open the appropriate command prompt as configured by your compiler package | ||
| 192 | (for example, the MSVC Command Prompt for Visual Studio). | ||
| 193 | |||
| 194 | Note that for compiling binary rocks that have dependencies on other | ||
| 195 | libraries, LuaRocks needs to be able to find [external | ||
| 196 | dependencies](paths_and_external_dependencies.md). | ||
| 197 | |||
| 198 | |||
