RMagick
=======

[![GemVersion](https://img.shields.io/gem/v/rmagick.svg?style=flat)](https://rubygems.org/gems/rmagick)
![CI](https://github.com/rmagick/rmagick/workflows/CI/badge.svg)

Table of Contents
-----------------

- [Introduction](#introduction)
- [Prerequisites](#prerequisites)
- [Installing RMagick](#installing-rmagick)
- [Using RMagick](#using-rmagick)
- [Things that can go wrong](#things-that-can-go-wrong)
- [Upgrading](#upgrading)
- [More samples](#more-samples)
- [Reporting Bugs](#reporting-bugs)
- [Development Setup](#development-setup)
- [Credits](#credits)
- [License](#license)
- [Releasing](#releasing)

Introduction
------------

RMagick is an interface between the Ruby programming language and the
ImageMagick image processing library.

Prerequisites
-------------

These prerequisites are required for the latest version of RMagick.

**OS**
- Linux
- \*BSD
- macOS
- Windows
- Other \*nix-like systems

**C++ compiler**
- RMagick 5.4.0 or later requires a C++ compiler.

**Ruby**
- Version 3.2 or later.

You can get Ruby from <https://www.ruby-lang.org>.

Ruby must be able to build C-Extensions (e.g. MRI, not JRuby)

**ImageMagick**
- Version 6.9 or later (for ImageMagick 6 users).
- Version 7.1 or later (for ImageMagick 7 users).

You can get ImageMagick from <https://imagemagick.org>.

### Linux
#### Debian-based
On Debian-based, you can run:

```sh
apt install libmagickwand-dev
```

#### RHEL-compatible
On RHEL-compatible, you can run:

```sh
dnf install ImageMagick-devel
```

#### Arch Linux
On Arch Linux, you can run:

```sh
pacman -Syy imagemagick
```

#### Alpine Linux
On Alpine Linux, you can run:

```
apk add imagemagick imagemagick-dev imagemagick-libs
```

or you can run if you would like to use ImageMagick 6:

```
apk add imagemagick6 imagemagick6-dev imagemagick6-libs
```

### macOS
On macOS, you can run:

```sh
brew install imagemagick
```

or you can run if you would like to use ImageMagick 6:

```sh
brew install imagemagick@6
```

### Windows
1. Install latest Ruby+Devkit package which you can get from [RubyInstaller for Windows](https://rubyinstaller.org).
2. Download `ImageMagick-7.XXXX-Q16-x64-dll.exe` (not, `ImageMagick-7.XXXX-Q16-x64-static.exe`) binary from [Windows Binary Release](https://imagemagick.org/script/download.php#windows), or you can download ImageMagick 6 from [Windows Binary Release](https://legacy.imagemagick.org/script/download.php#windows).
3. Install ImageMagick. You need to turn on checkboxes `Add application directory to your system path` and `Install development headers for C and C++` in an installer for RMagick.
<img width="75%" src="https://github.com/rmagick/rmagick/assets/199156/494e7963-cca5-4cb5-b28a-6c4d76adce5d" />

If you want to install ImageMagick using [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/), run the following command:

```sh
winget install ImageMagick.ImageMagick --custom /TASKS=modifypath,install_Devel
```

If you want to install ImageMagick using [Chocolatey](https://chocolatey.org/), run the following command:

```sh
choco install imagemagick -PackageParameters InstallDevelopmentHeaders=true
```

### Windows (via MSYS2 / RubyInstaller)

If you are using RubyInstaller for Windows, you can easily install ImageMagick using MSYS2's `pacman` package manager.
Open your command prompt and run the appropriate command for your architecture to install ImageMagick:

For x64:

```sh
ridk exec pacman -S mingw-w64-ucrt-x86_64-imagemagick
```
For ARM64:

```
ridk exec pacman -S mingw-w64-clang-aarch64-imagemagick
```

Installing RMagick
------------------

> [!NOTE]
> When you update the ImageMagick version, we have recommended you should re-install RMagick.

### Installing via Bundler

Add to your `Gemfile`:

```rb
gem 'rmagick'
```

Then run:

```sh
bundle install
```

For Windows, you need to run using [ridk tool](https://github.com/oneclick/rubyinstaller2/wiki/The-ridk-tool):
```sh
ridk exec bundle install
```

### Installing via RubyGems

Run:

```sh
gem install rmagick
```

For Windows, you need to run using [ridk tool](https://github.com/oneclick/rubyinstaller2/wiki/The-ridk-tool):
```sh
ridk exec gem install rmagick
```

### Versioning

RMagick is versioned according to Semantic Versioning.
Versions >= 7 work on Ruby >= 3.2 only.
For older Ruby versions, use RMagick `~> 6.0`.

Using RMagick
-------------

Require RMagick in your project as follows:

```rb
require 'rmagick'
```

See <https://rmagick.github.io/usage.html> for links to more information.

Things that can go wrong
------------------------

The [RMagick installation FAQ][faq] has answers to the most commonly reported
problems, though may be out of date.

### Can't install RMagick. Can't find libMagickCore-XXXX.so or one of the dependent libraries. Check the mkmf.log file for more detailed information

Typically this message means that one or more of the libraries that ImageMagick
depends on hasn't been installed. Examine the mkmf.log file in the ext/RMagick
subdirectory of the installation directory for any error messages. These
messages typically contain enough additional information for you to be able to
diagnose the problem. Also see [this FAQ][libmagick-faq].

### Cannot open shared object file

If you get a message like this:

```sh
... /core_ext/kernel_require.rb>:136:in `require': cannot load such file -- RMagick2.so (LoadError)
  (snip)
```

you probably do not have the directory in which the ImageMagick library
is installed in your load path. An easy way to fix this is to define
the directory in the `LD_LIBRARY_PATH` environment variable. For
example, suppose you installed the ImageMagick library `libMagickCore-XXXX.so` in
`/usr/local/lib`. (By default this is where it is installed.) Create the
`LD_LIBRARY_PATH` variable like this:

```sh
export LD_LIBRARY_PATH=/usr/local/lib
```

On Linux, see `ld(1)` and `ld.so(8)` for more information. On other operating
systems, see the documentation for the dynamic loading facility.

This operation might not be required when you can use 4.2.5 or later.

### Segmentation fault

Default stack size of your operating system might be too small. Try removing
the limit with this command:

```sh
ulimit -s unlimited
```

Upgrading
---------

If you upgrade to a newer release of ImageMagick, make sure you're using a
release of RMagick that supports that release. It's safe to install a new
release of RMagick over an earlier release.

More samples
------------

You can find more sample RMagick programs in the [/examples](https://github.com/rmagick/rmagick/tree/main/examples) and [/doc/ex](https://github.com/rmagick/rmagick/tree/main/doc/ex) directories. These
programs are not installed in the RMagick documentation tree.

Reporting bugs
--------------

Please report bugs in RMagick, its documentation, or its installation programs
via the bug tracker on the [RMagick issues page][issues].

However, We can't help with Ruby installation and configuration or ImageMagick
installation and configuration. Information about reporting problems and
getting help for ImageMagick is available at the [ImageMagick
website][imagemagick] or the [ImageMagick Discussions][imagemagick-discussions].

Development Setup
-----------------

In order to minimize issues on your local machine, we recommend that you make
use of [Dev Containers](https://containers.dev/). This provides a consistent, pre-configured development environment using Docker.

Steps to get up and running with a passing build are as follows:

### 1) Set up the prerequisites

To use Dev Containers, you will need to install the following tools on your machine:
* [Docker Desktop](https://www.docker.com/products/docker-desktop/) (or Docker Engine)
* [Visual Studio Code](https://code.visualstudio.com/)
* [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) for VS Code

### 2) Clone RMagick and open the container

Clone the repository to your local machine and open it in VS Code:

```sh
git clone https://github.com/rmagick/rmagick.git # or your fork
cd rmagick
code .
```

Once VS Code opens, it should detect the `.devcontainer` configuration and prompt you to "Reopen in Container".

During this process, VS Code will ask you to select a `devcontainer.json` configuration. You can choose the environment you want to develop and test against:

* ImageMagick 7 (`.devcontainer/devcontainer.json`) - Recommended for the latest development.
* ImageMagick 6 (`.devcontainer/ImageMagick6/devcontainer.json`) - For testing against the legacy ImageMagick 6 API.

Select the one you need, and VS Code will automatically build the corresponding Docker image and start your isolated development environment. This may take a few minutes the first time.

### 3) Build RMagick
Once the container is running and the ImageMagick setup is complete, open a new terminal inside VS Code.
Install the Ruby dependencies and compile the RMagick extension:

```
$ bundle install
$ bundle exec rake
```

And you're all set! The codebase on your local machine is automatically synced with the container. You can edit files using your local VS Code, and run tests seamlessly within the integrated container terminal.

Credits
-------

**Authors:** Tim Hunter, Omer Bar-or, Benjamin Thomas

Thanks to [ImageMagick Studio LLC][imagemagick] for ImageMagick and for hosting
the RMagick documentation.

License
-----------

[MIT License](LICENSE)

Releasing
---------

1.  Update ChangeLog
2.  Edit `lib/rmagick/version.rb`
3.  Are the tests passing? Run `rake` again just to be sure.
4.  `rake release`

[issues]: https://github.com/rmagick/rmagick/issues
[libmagick-faq]: https://web.archive.org/web/20140512193354/https://rmagick.rubyforge.org/install-faq.html#libmagick
[faq]: https://web.archive.org/web/20140512193354/https://rmagick.rubyforge.org/install-faq.html
[imagemagick]: https://imagemagick.org
[imagemagick-discussions]: https://github.com/ImageMagick/ImageMagick/discussions
